package com.nebulas.nettyhex.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class HexDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        // 读取字节数据并转换为十六进制字符串
        byte[] bytes = new byte[in.readableBytes()];
        in.readBytes(bytes);
        //String hexData =bytesToHex(bytes);
        String hexData = parseData(bytes);
        out.add(hexData); // 将十六进制字符串传递给下一个处理器
    }

    // 将字节数组转换为十六进制字符串
    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    private String parseData(byte[] data) {
        // 使用 BigInteger 解析数据，默认使用大端字节序
        BigInteger bigInt = new BigInteger(1, data); // 1 表示是无符号整数
        StringBuilder sb = new StringBuilder();
        // 如果你想从字节数组中提取不同的数据，可以逐个字段提取
        // 假设协议中前 6 字节是帧头
        byte[] frameHeader = new byte[6];
        System.arraycopy(data, 0, frameHeader, 0, 6);
        sb.append(bytesToHex(frameHeader)).append(",");

        // 打印帧头
        System.out.print("帧头: " + bytesToHex(frameHeader)+"\t");

        // 提取设备ID（1字节）
        byte deviceId = data[6];
        System.out.print("设备ID: " + deviceId+"\t");
        sb.append(deviceId).append(",");

        // 提取设备类型（1字节）
        byte deviceType = data[7];
        char charDevicetype = (char) deviceType;
        System.out.print("设备类型: " + charDevicetype+"\t");
        sb.append(charDevicetype).append(",");


        // 提取经度（4字节）
        byte[] longitudeBytes = new byte[4];
        System.arraycopy(data, 8, longitudeBytes, 0, 4);

        BigInteger longitude = new BigInteger(1,longitudeBytes ); // 使用 BigInteger 转换
        System.out.print("经度: " + longitude+"\t");
        sb.append(longitude).append(",");


        // 提取纬度（4字节）
        byte[] latitudeBytes = new byte[4];
        System.arraycopy(data, 12, latitudeBytes, 0, 4);
        BigInteger latitude = new BigInteger(1, latitudeBytes);
        System.out.print("纬度: " + latitude+"\t");
        sb.append(latitude).append(",");


        //  值1（8字节）
        byte[] valueBytes1 = new byte[8];
        System.arraycopy(data, 16, valueBytes1, 0, 8);
        double value1 =  hexStringToDouble(valueBytes1);
        System.out.print("值1: " + value1+"\t");
        sb.append(value1).append(",");



        //  值2（8字节）
        byte[] valueBytes2 = new byte[8];
        System.arraycopy(data, 24, valueBytes2, 0, 8);
        double value2 =  hexStringToDouble(valueBytes2);
        System.out.print("值2: " + value2+"\t");
        sb.append(value2).append(",");


        //  值3（8字节）
        byte[] valueBytes3 = new byte[8];
        System.arraycopy(data, 32, valueBytes3, 0, 8);
        double value3 =  hexStringToDouble(valueBytes3);
        System.out.print("值3: " + value3+"\t");
        sb.append(value3).append(",");



        //  值4（8字节）
        byte[] valueBytes4 = new byte[8];
        System.arraycopy(data, 40, valueBytes4, 0, 8);
        double value4 =  hexStringToDouble(valueBytes4);
        System.out.print("值4: " + value4+"\t");
        sb.append(value4).append(",");



        // 提取设备状态（1字节）
        byte deviceStatus = data[41];
        char charDeviceStatus = (char) deviceStatus;
        System.out.print("设备状态: " + charDeviceStatus+"\t");
        sb.append(charDeviceStatus).append(",");


        byte[] frequencyBandBytes = new byte[8];
        System.arraycopy(data, 42, frequencyBandBytes, 0, 8);
        double frequencyBand =  hexStringToDouble(frequencyBandBytes);
        System.out.print("频段: " + frequencyBand+"\t");
        sb.append(frequencyBand).append(",");


        byte[] signalCountBytes = new byte[2];
        System.arraycopy(data, 50, signalCountBytes, 0, 2);
       // double signalCount =  hexStringToDouble(signalCountBytes);
        BigInteger signalCount = new BigInteger(1,signalCountBytes ); // 使用 BigInteger 转换
        System.out.print("信号数: " + signalCount+"\t");
        sb.append(signalCount).append(",");


        byte[] unSingNedBytes = new byte[4];
        System.arraycopy(data, 53, unSingNedBytes, 0, 4);
        BigInteger unSingNed = new BigInteger(1, unSingNedBytes);
        System.out.print("校验字: " + unSingNed+"\n");
        sb.append(unSingNed);
        return sb.toString();
    }

    public static double hexStringToDouble( byte[] bytes) {
        // 将字节数组转换为long类型的位表示
        long bits = 0;
        for (int i = (bytes.length - 1); i >= 0; i--) {
            bits = (bits << 8) | (bytes[i] & 0xff);
        }
        // 将位表示转换为double类型
        return Double.longBitsToDouble(bits);
    }
}
